﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Shell32;

namespace Folder_Control
{

    public abstract class SpecialFolder : Item
    {
        public override List<Item> onClick()
        {
            return GetItems();
        }

        public abstract List<Item> GetItems();
    }

    public class MyComputer : SpecialFolder
    {
        private List<string> SpecialFolderItems;

        public MyComputer(Icon icon, List<string> specialFolderItems)
        {
            Name = "Computer";
            Path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            Type = ItemType.SpecialFolder;
            Icon = icon;
            SpecialFolderItems = specialFolderItems;
        }

        public override List<Item> GetItems()
        {
            Icons icons = new Icons(Icons.SystemImageListSize.ExtraLargeIcons);

            List<Item> specialFolderItems = new List<Item>();

            foreach (var folderItem in SpecialFolderItems)
            {
                specialFolderItems.Add(new Folder(folderItem, folderItem, icons.GetFolderIcon(folderItem)));
            }

            return specialFolderItems;
        }
    }

    public class RecycleBin : SpecialFolder
    {
        public RecycleBin(Icon icon)
        {
            Name = "Recycle Bin";
            Path = String.Empty;
            Type = ItemType.SpecialFolder;
            Icon = icon;
        }

        public override List<Item> GetItems()
        {
            Shell shell = new Shell();

            Shell32.Folder recycleBin = shell.NameSpace(10);

            Icons icons = new Icons(Icons.SystemImageListSize.ExtraLargeIcons);

            List<Item> specialFolderItems = new List<Item>();

            foreach (FolderItem2 folderItem in recycleBin.Items())
            {
                if (System.IO.File.Exists(folderItem.Path))
                {
                    specialFolderItems.Add(new File(folderItem.Name, folderItem.Path, icons.GetIcon(folderItem.Path)));
                }
                else if (Directory.Exists(folderItem.Path)) // else if the target of the link is a directory return a Folder object
                {
                    specialFolderItems.Add(new Folder(folderItem.Name, folderItem.Path, icons.GetFolderIcon(folderItem.Path)));
                }     
            }
      
            return specialFolderItems;
        }
    }

    public class SpecialFolders
    {
        private static Dictionary<Icons.SpecialFolders, Icon> specialFolderIcons;
        public static Dictionary<Icons.SpecialFolders, Icon> SpecialFolderIcons
        {
            get
            {
                if (specialFolderIcons == null)
                {
                    Icons icons = new Icons(Icons.SystemImageListSize.ExtraLargeIcons);
                    specialFolderIcons = new Dictionary<Icons.SpecialFolders, Icon>
                    {
                        {Icons.SpecialFolders.MyComputer, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyComputer)},
                        {Icons.SpecialFolders.ControlPanel, icons.GetSpecialFolderIcon(Icons.SpecialFolders.ControlPanel)},
                        {Icons.SpecialFolders.MyDocuments, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyDocuments)},
                        {Icons.SpecialFolders.Network, icons.GetSpecialFolderIcon(Icons.SpecialFolders.Network)},
                        {Icons.SpecialFolders.RecycleBin, icons.GetSpecialFolderIcon(Icons.SpecialFolders.RecycleBin)}
                    };

                    icons.Dispose();
                }

                return specialFolderIcons;
            }
        }

        private static Dictionary<Icons.SpecialFolders, Icon> smallSpecialFolderIcons;
        public static Dictionary<Icons.SpecialFolders, Icon> SmallSpecialFolderIcons
        {
            get
            {
                if (smallSpecialFolderIcons == null)
                {
                    Icons icons = new Icons(Icons.SystemImageListSize.SmallIcons);
                    smallSpecialFolderIcons = new Dictionary<Icons.SpecialFolders, Icon>
                    {
                        {Icons.SpecialFolders.MyComputer, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyComputer)},
                        {Icons.SpecialFolders.Desktop, icons.GetSpecialFolderIcon(Icons.SpecialFolders.Desktop)},
                        {Icons.SpecialFolders.ControlPanel, icons.GetSpecialFolderIcon(Icons.SpecialFolders.ControlPanel)},
                        {Icons.SpecialFolders.MyDocuments, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyDocuments)},
                        {Icons.SpecialFolders.Network, icons.GetSpecialFolderIcon(Icons.SpecialFolders.Network)},
                        {Icons.SpecialFolders.RecycleBin, icons.GetSpecialFolderIcon(Icons.SpecialFolders.RecycleBin)},
                        {Icons.SpecialFolders.MyMusic, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyMusic)},
                        {Icons.SpecialFolders.MyPictures, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyPictures)},
                        {Icons.SpecialFolders.MyVideos, icons.GetSpecialFolderIcon(Icons.SpecialFolders.MyVideos)}
                    };

                    icons.Dispose();
                }

                return smallSpecialFolderIcons;
            }
        }

        public static Dictionary<Icons.SpecialFolders, Item> SpecialFoldersForDesktopDictionary
            = new Dictionary<Icons.SpecialFolders, Item>
            {
                {Icons.SpecialFolders.MyComputer, new MyComputer(SpecialFolderIcons[Icons.SpecialFolders.MyComputer], Directory.GetLogicalDrives().ToList())},
                {Icons.SpecialFolders.MyDocuments, new Folder("My Documents", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), SpecialFolderIcons[Icons.SpecialFolders.MyDocuments])},
                {Icons.SpecialFolders.RecycleBin, new RecycleBin(SpecialFolderIcons[Icons.SpecialFolders.RecycleBin])},
            };

        public static Dictionary<Icons.SpecialFolders, Item> SpecialFoldersForTreeViewDictionary
            = new Dictionary<Icons.SpecialFolders, Item>
            {
                {Icons.SpecialFolders.MyComputer, new MyComputer(SmallSpecialFolderIcons[Icons.SpecialFolders.MyComputer], Directory.GetLogicalDrives().ToList())},
                {Icons.SpecialFolders.Desktop, new Folder("Desktop", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), SmallSpecialFolderIcons[Icons.SpecialFolders.Desktop])},
                {Icons.SpecialFolders.MyDocuments, new Folder("My Documents", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), SmallSpecialFolderIcons[Icons.SpecialFolders.MyDocuments])},
                {Icons.SpecialFolders.MyPictures, new Folder("My Pictures", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), SmallSpecialFolderIcons[Icons.SpecialFolders.MyPictures])},
                {Icons.SpecialFolders.MyMusic, new Folder("My Music", Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), SmallSpecialFolderIcons[Icons.SpecialFolders.MyMusic])},
                {Icons.SpecialFolders.MyVideos, new Folder("My Video", Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), SmallSpecialFolderIcons[Icons.SpecialFolders.MyVideos])},
                {Icons.SpecialFolders.RecycleBin, new RecycleBin(SmallSpecialFolderIcons[Icons.SpecialFolders.RecycleBin])},
            };
    }
}
